/**
 * @filename client.js
 *
 * @module
 *
 * @author Justin <zhou78620051@126.com>
 * @version 1
 * @time 2017/2/1 0:23
 */

"use strict";

const WebSocket = require('ws');

const ws = new WebSocket('ws://localhost:3000/', {
    perMessageDeflate: false,
    origin: 'http://localhost:3000/'
});

ws.on('open', function open() {
    ws.send('something');
});

ws.on('message', function incoming(data, flags) {
    // flags.binary will be set if a binary data is received.
    // flags.masked will be set if the data was masked.

    data = data.toString();
    data = JSON.parse(data);

    var timeRecv = new Date().valueOf();
    var timeSent = data.timestamp;
    // 检测网络通信时差
    console.log("[ %s ] data [ %s ] delta [ %s ]", timeRecv, timeSent, timeRecv - timeSent);
});